home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Games / DroneZone / DZEvents.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  5.9 KB  |  308 lines  |  [TEXT/MPS ]

  1. /*
  2.  *    File:        DZEvents.c
  3.  *
  4.  *    Contents:    Handles the events.
  5.  *
  6.  *    Copyright © 1996 Apple Computer, Inc.
  7.  */
  8.  
  9. #include <assert.h>
  10.  
  11. #include <AppleEvents.h>
  12. #include <Events.h>
  13. #include <Menus.h>
  14. #include <ToolUtils.h>
  15. #include <Types.h>
  16.  
  17. #include "DZDisplay.h"
  18. #include "DZEvents.h"
  19. #include "DZGame.h"
  20. #include "DZMain.h"
  21. #include "DZMenu.h"
  22.  
  23.  
  24. static void Events_MouseDown(
  25.     const EventRecord*        inEvent);
  26.  
  27. static void Events_KeyDown(
  28.     const EventRecord*        inEvent);
  29.  
  30. static void Events_AutoKey(
  31.     const EventRecord*        inEvent);
  32.  
  33. static void Events_Update(
  34.     const EventRecord*        inEvent);
  35.  
  36. static void Events_Activate(
  37.     const EventRecord*        inEvent);
  38.  
  39. static void Events_OS(
  40.     const EventRecord*        inEvent);
  41.  
  42. static void Events_HighLevel(
  43.     const EventRecord*        inEvent);
  44.  
  45.  
  46. /* =============================================================================
  47.  *        Events_Init (external)
  48.  *
  49.  *    Initializes the events stuff.
  50.  * ========================================================================== */
  51. void Events_Init(
  52.     void)
  53. {
  54.     FlushEvents(everyEvent, 0);
  55. }
  56.  
  57.  
  58. /* =============================================================================
  59.  *        Events_Exit (external)
  60.  *
  61.  *    Prepares for exit.
  62.  * ========================================================================== */
  63. void Events_Exit(
  64.     void)
  65. {
  66. }
  67.  
  68.  
  69. /* =============================================================================
  70.  *        Events_Process (external)
  71.  *
  72.  *    Processes any pending events.
  73.  * ========================================================================== */
  74. void Events_Process(
  75.     void)
  76. {
  77.     EventRecord                ev;
  78.     
  79.     while (GetNextEvent(everyEvent, &ev))
  80.     {
  81.         switch (ev.what)
  82.         {
  83.             case mouseDown:
  84.                 Events_MouseDown(&ev);
  85.             break;
  86.             
  87.             case keyDown:
  88.                 Events_KeyDown(&ev);
  89.             break;
  90.             
  91.             case autoKey:
  92.                 Events_AutoKey(&ev);
  93.             break;
  94.             
  95.             case updateEvt:
  96.                 Events_Update(&ev);
  97.             break;
  98.             
  99.             case activateEvt:
  100.                 Events_Activate(&ev);
  101.             break;
  102.             
  103.             case osEvt:
  104.                 Events_OS(&ev);
  105.             break;
  106.             
  107.             case kHighLevelEvent:
  108.                 Events_HighLevel(&ev);
  109.             break;
  110.             
  111.         }
  112.     }
  113.     
  114.     SystemTask();
  115. }
  116.  
  117.  
  118. /* =============================================================================
  119.  *        Events_MouseDown (internal)
  120.  *
  121.  *    Processes a mouse-down event.
  122.  * ========================================================================== */
  123. void Events_MouseDown(
  124.     const EventRecord*        inEvent)
  125. {
  126.     short                     part;
  127.     WindowPtr                wind;
  128.     long                    menu;
  129.     Rect                    limits;
  130.     long                    size;
  131.     
  132.     assert(inEvent != NULL);
  133.     
  134.     part = FindWindow(inEvent->where, &wind);
  135.     
  136.     switch (part)
  137.     {
  138.         case inMenuBar:
  139.             menu = MenuSelect(inEvent->where);
  140.             Menu_Select(HiWord(menu), LoWord(menu));
  141.         break;
  142.             
  143.         case inSysWindow:
  144.             SystemClick(inEvent, wind);
  145.         break;
  146.             
  147.         case inDrag:
  148.             DragWindow(wind, inEvent->where, &qd.screenBits.bounds);
  149.         break;
  150.             
  151.         case inGoAway:
  152.             Main_LastRoundup();
  153.         break;
  154.                 
  155.         case inContent:
  156.             // nothing yet...
  157.         break;
  158.             
  159.         case inGrow:
  160.             limits.top    = 64;
  161.             limits.left   = 64;
  162.             limits.bottom = 4096;
  163.             limits.right  = 4096;
  164.             
  165.             size = GrowWindow(wind, inEvent->where, &limits);
  166.             
  167.             if (size != 0)
  168.             {
  169.                 SetPort(wind);
  170.                 EraseRect(&wind->portRect);
  171.                 SizeWindow(wind, LoWord(size), HiWord(size), true);
  172.                 InvalRect(&wind->portRect);
  173.                 
  174.                 if (wind == Display_GetWindow())
  175.                 {
  176.                     Display_Resize();
  177.                 }
  178.             } 
  179.         break;
  180.     }
  181. }
  182.  
  183.  
  184. /* =============================================================================
  185.  *        Events_KeyDown (internal)
  186.  *
  187.  *    Processes a key-down event.
  188.  * ========================================================================== */
  189. void Events_KeyDown(
  190.     const EventRecord*        inEvent)
  191. {
  192.     unsigned long            ch;
  193.     unsigned long            cap;
  194.     long                    menu;
  195.     
  196.     assert(inEvent != NULL);
  197.     
  198.     ch = inEvent->message & charCodeMask;
  199.     cap = (inEvent->message & keyCodeMask) >> 8;
  200.     
  201.     if ((inEvent->modifiers & cmdKey) != 0)
  202.     {
  203.         menu = MenuKey(ch);
  204.         Menu_Select(HiWord(menu), LoWord(menu));
  205.     }
  206.     else if (ch == 0x1b)
  207.     {
  208.         if (Game_GetState() == kGameState_Paused)
  209.         {
  210.             Game_SetState(kGameState_Playing);
  211.         }
  212.     }
  213. }
  214.  
  215.  
  216. /* =============================================================================
  217.  *        Events_AutoKey (internal)
  218.  *
  219.  *    Processes an auto-key event.
  220.  * ========================================================================== */
  221. void Events_AutoKey(
  222.     const EventRecord*        inEvent)
  223. {
  224.     // Do nothing for now
  225. }
  226.  
  227.  
  228. /* =============================================================================
  229.  *        Events_Update (internal)
  230.  *
  231.  *    Processes an update event.
  232.  * ========================================================================== */
  233. void Events_Update(
  234.     const EventRecord*        inEvent)
  235. {
  236.     WindowPtr                wind;
  237.     
  238.     assert(inEvent != NULL);
  239.     
  240.     wind = (WindowPtr) inEvent->message;
  241.     
  242.     BeginUpdate(wind);
  243.     
  244.     if (wind == Display_GetWindow())
  245.     {
  246.         Display_DrawGrow();
  247.         Display_DrawContents();
  248.     }
  249.     
  250.     EndUpdate(wind);
  251. }
  252.  
  253.  
  254. /* =============================================================================
  255.  *        Events_Activate (internal)
  256.  *
  257.  *    Processes an activate event.
  258.  * ========================================================================== */
  259. void Events_Activate(
  260.     const EventRecord*        inEvent)
  261. {
  262.     WindowPtr                wind;
  263.     
  264.     assert(inEvent != NULL);
  265.     
  266.     wind = (WindowPtr) inEvent->message;
  267.     
  268.     if (wind == Display_GetWindow())
  269.     {
  270.         Display_Activate((inEvent->modifiers & activeFlag) != 0);
  271.     }
  272. }
  273.  
  274.  
  275. /* =============================================================================
  276.  *        Events_OS (internal)
  277.  *
  278.  *    Processes an OS event.
  279.  * ========================================================================== */
  280. void Events_OS(
  281.     const EventRecord*        inEvent)
  282. {
  283.     assert(inEvent != NULL);
  284.     
  285.     if ((inEvent->message >> 24) & 0xFF == suspendResumeMessage)
  286.     {
  287.         Display_Activate((inEvent->message & resumeFlag) != 0);
  288.     }
  289. }
  290.  
  291.  
  292. /* =============================================================================
  293.  *        Events_HighLevel (internal)
  294.  *
  295.  *    Processes a high-level event.
  296.  * ========================================================================== */
  297. void Events_HighLevel(
  298.     const EventRecord*        inEvent)
  299. {
  300.     assert(inEvent != NULL);
  301.     
  302.     AEProcessAppleEvent(inEvent);
  303. }
  304.  
  305.  
  306.  
  307.  
  308.